package com.invengo.module_check.ui.fragment.opeartion;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.module_check.R;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

public class OpeartionViewModel extends BaseViewModel<BusinessRepsitory> {
    public SingleLiveEvent<Boolean> state = new SingleLiveEvent<>();
    public SingleLiveEvent<String> stateTitle = new SingleLiveEvent<>();
    public SingleLiveEvent<String> opTip = new SingleLiveEvent<>();
    public SingleLiveEvent<String> opSection = new SingleLiveEvent<>();
    public SingleLiveEvent<String> shelfInfo = new SingleLiveEvent<>();


    public OpeartionViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
    }

    public UIChangeObservable ui = new UIChangeObservable();


    public class UIChangeObservable {
        public SingleLiveEvent<Void> launchInfoEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Void> launchHomeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Void> launchWorkEvent = new SingleLiveEvent<>();
    }

    public BindingCommand launchInfo = new BindingCommand(() -> {
        ui.launchInfoEvent.call();
    });
    public BindingCommand back2work = new BindingCommand(() -> {
        ui.launchWorkEvent.call();
    });
    public BindingCommand launchMain = new BindingCommand(() -> {
        ui.launchHomeEvent.call();
    });


    public void showListing(boolean state, String opSection, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "上架成功" : "上架失败");
        this.opSection.postValue(state ? opSection : errorInfo);
    }

    public void showCharge(boolean state, String opSection, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "倒架成功" : "倒架失败");
        this.opTip.postValue(state ?  "请将倒架"+getApplication().getString(R.string.book)+"移动目标层位" : errorInfo);
        this.opSection.postValue("目标层位：" + (state ? opSection : errorInfo));
    }

    public void showForce(boolean state, String opSection, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "强制定位成功" : "强制定位失败");
        this.opTip.postValue(state ? "请将"+getApplication().getString(R.string.book)+"移动目标层位" : errorInfo);
        this.opSection.postValue("目标层位：" + (state ? opSection : errorInfo));
    }

    public void showFirstCheck(boolean state, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "更新成功" : "更新失败");
        this.opSection.postValue("");
    }


    public void showRevoke(boolean state, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "撤销强制定位成功" : "撤销强制定位失败");
        this.opTip.postValue(state ? "请将"+getApplication().getString(R.string.book)+"移至各自的层位或下架" : errorInfo);
    }

    public void showInventory(boolean state, String opSection, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "盘点成功" : "盘点失败");
        this.opSection.postValue(state ? opSection : errorInfo);
    }

    public void showParaller(boolean state, String opSection, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "顺架完成" : "顺架失败");
        this.opSection.postValue(state ? opSection : errorInfo);
    }

    public void showFirstPos(boolean state, String opSection, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "首书定位完成" : "首书定位失败");
        this.opSection.postValue(state ? opSection : errorInfo);
    }

    public void showDown(boolean state, Map<String, List<BookBean>> collect, String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "下架成功" : "下架失败");
        this.opTip.postValue(state ? "请将下架图书从架位上取走" : errorInfo);
        StringBuffer sb = new StringBuffer();
        int i = 0;
        for (String key : collect.keySet()) {
            if (i == 0) {
                sb.append(key);
                sb.append("-");
            } else if (i == collect.size() - 1) {
                sb.append(key);
            }
            i++;
        }
        this.opSection.postValue(sb.toString());
    }
    public void showDown(boolean state,  String errorInfo) {
        this.state.postValue(state);
        this.stateTitle.postValue(state ? "下架成功" : "下架失败");
        this.opTip.postValue(state ? "请将下架图书从架位上取走" : errorInfo);
    }
}
